package com.kzj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kzj.dao.ClassroomDao;
import com.kzj.domain.Classroom;
import com.kzj.domain.ElectiveCourse;
import com.kzj.domain.Examination;
import com.kzj.domain.vo.FreeClassroomDTO;
import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.re.Result;
import com.kzj.service.ClassroomService;
import com.kzj.service.EletiveCourseService;
import com.kzj.service.ExaminationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomDao, Classroom> implements ClassroomService {
    @Autowired
    EletiveCourseService eletiveCourseService;
    @Autowired
    ExaminationService examinationService;
    @Override
    public Result getFreeClassroom(FreeClassroomDTO freeClassroomDTO) {
        List<ElectiveCourse> electiveCourses = eletiveCourseService.query().eq("curriculum_time",freeClassroomDTO.getCurriculumTime()).list();
        List<String> noFreeClassroomIds=new ArrayList<>();
        for (ElectiveCourse electiveCourse : electiveCourses) {
            if(freeClassroomDTO.getStartWeek()>electiveCourse.getEndWeek()||freeClassroomDTO.getEndWeek()<electiveCourse.getStartWeek())
                continue;
            if(freeClassroomDTO.getSchooltime().equals(electiveCourse.getSchooltime()))
            {
                noFreeClassroomIds.add(electiveCourse.getClassroomId());
            }
        }
        List<Examination> examinations=new ArrayList<>();
        for(int i=freeClassroomDTO.getStartWeek();i<=freeClassroomDTO.getEndWeek();i++)
        {
            String startTime=i+GlobalConstant.COURSE_SCHOOL_TIME_SPLIT+freeClassroomDTO.getSchooltime();
            examinations.addAll(examinationService.query().eq("start_time", startTime).list());
        }
        for (Examination examination : examinations) {
            noFreeClassroomIds.add(examination.getClassroomId());
        }
        List<Classroom> classrooms = this.query().notIn(!noFreeClassroomIds.isEmpty(),"id", noFreeClassroomIds).list();
        return Result.success(classrooms);
    }
}
